package org.ssp.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import org.ssp.base.ResultSetHanlder;
import org.ssp.dao.StudyDao;
import org.ssp.pojo.PartJob;
import org.ssp.pojo.Study;
import org.ssp.util.DBUtil;

public class StudyImpl implements StudyDao{
	@Override
	public void add(Study study) {
		String sql="insert into study(title,content,datetime) values(?,?,curdate());";
		DBUtil.update(sql, study.getTitle(),study.getContent());
	}
	@Override
	public ArrayList<Study> findAll(int offset, int rows) {
		String sql="select * from study limit ?,?;";
		return (ArrayList<Study>)DBUtil.query(sql, new ResultSetHanlder() {
			
			@Override
			public Object doHandler(ResultSet rs) {
				ArrayList<Study> list=new ArrayList<Study>();
				try {
					while(rs.next()){
						Study s=new Study();
						s.setId(rs.getInt("id"));
						s.setTitle(rs.getString("title"));
						s.setContent(rs.getString("content"));
						s.setDate(rs.getDate("datetime"));
						list.add(s);
					}
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					return null;
				}
				return list;
			}
		}, offset,rows);
	}
	@Override
	public int findAll() {
		String sql="select * from study;";
		return (Integer)DBUtil.query(sql, new ResultSetHanlder() {
			
			@Override
			public Object doHandler(ResultSet rs) {
				Integer total=0;
				try {
					while(rs.next()){
						total+=1;
					}
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					return null;
				}
				return total;
			}
		}, null);
	}
	@Override
	public Study findById(int id) {
		String sql="select * from study where id=?;";
		return (Study)DBUtil.query(sql, new ResultSetHanlder() {
			
			@Override
			public Object doHandler(ResultSet rs) {
				Study study=null;
				try {
					if(rs.next()){
						study=new Study();
						study.setId(rs.getInt("id"));
						study.setTitle(rs.getString("title"));
						study.setContent(rs.getString("content"));
						study.setDate(rs.getDate("datetime"));
						System.out.println("存在");
					}
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					return null;
				}
				return study;
			}
		}, id);
	
	}
	@Override
	public void update(Study study) {
		String sql="update study set title=?,content=? where id=?";
		DBUtil.update(sql, study.getTitle(),study.getContent(),study.getId());
	}
	@Override
	public void delete(int id) {
		String sql="delete from study where id=?;";
		DBUtil.update(sql, id);
	}
	@Override
	public ArrayList<Study> find10(int offset,int rows){
		String sql="select * from study limit ?,?;";
		return (ArrayList<Study>)DBUtil.query(sql, new ResultSetHanlder() {
			
			@Override
			public Object doHandler(ResultSet rs) {
				ArrayList<Study> list=new ArrayList<Study>();
				try {
					while(rs.next()){
						Study s=new Study();
						s.setId(rs.getInt("id"));
						s.setTitle(rs.getString("title"));
						s.setContent(rs.getString("content"));
						s.setDate(rs.getDate("datetime"));
						list.add(s);
					}
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					return null;
				}
				return list;
			}
		}, offset,rows);
	}
}
